/*
 * Copyright (c) 2005 -2008 Reliable Source, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Mozilla Public License v.1.1,
 * BSD, and Eclipse Public License v1.0 which accompanies this
 * distribution and is available at:
 *
 * 		http://www.eclipse.org/legal/epl-v10.html
 * 		http://www.opensource.org/licenses/bsd-license.php
 * 		http://www.mozilla.org/MPL/MPL-1.1.html
 *
 * Contributors:
 *     IBM Corporation	     -  http://ibm.com
 *     	- initial API and implementation
 *
 *     patientos.org         - http://patientos.org
 *     	- domain and database persistence layer
 *
 *     Reliable Source, Inc. - http://reliablesource.org
 *     	- Symbiosys Application Framework,
 *     	- Symbiosys Application, Symbiosys Product
 *
 */
package org.reliablesource.client.ui.symbiosys.platform;

import org.eclipse.swt.graphics.Point;

import org.eclipse.ui.IPerspectiveDescriptor;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;
import org.eclipse.ui.contexts.IContextActivation;
import org.eclipse.ui.contexts.IContextService;
import org.eclipse.ui.services.IServiceLocator;

/**
 *
 * ApplicationWorkbenchWindowAdvisor.java<br>
 * Created on: Jun 27, 2008<br>
 *
 * @author <a href="mailto:dennis.park@gmail.com">Dennis Park</a>
 *
 * @category
 * @since 1.0
 */
public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {
    /**
     * A workbench window context id
     */
    public static final String WINDOW_CONTEXT_ID = "window"; //$NON-NLS-1$

    /**
     * The default width of the workbench window when opened for the first time.
     */
    public static final int DEFAULT_WIDTH = 1050;

    /**
     * The default height of the workbench window when opened for the first
     * time.
     */
    public static final int DEFAULT_HEIGHT = 850;

    /**
     * A reference to the workbenchs' service locator framework.
     */
    protected IServiceLocator services;

    /**
     * A reference to the workbench context service.
     */
    protected IContextService contextService;

    /**
     * A reference to the workbenchs current context.
     */
    protected IContextActivation contextToken;

    public ApplicationWorkbenchWindowAdvisor(
	    IWorkbenchWindowConfigurer configurer) {
	super(configurer);
    }

    public ActionBarAdvisor createActionBarAdvisor(
	    IActionBarConfigurer configurer) {
	return new ApplicationActionBarAdvisor(configurer);
    }

    public void preWindowOpen() {
	IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
	configurer.setInitialSize(new Point(DEFAULT_WIDTH, DEFAULT_HEIGHT));
	configurer.setShowCoolBar(false);
	configurer.setShowStatusLine(false);
    }

    /*
     * (non-Javadoc)
     *
     * @see org.eclipse.ui.application.WorkbenchWindowAdvisor#postWindowOpen()
     */
    @Override
    public void postWindowOpen() {
	super.postWindowOpen();
	setContext();
	

	IPerspectiveDescriptor _pd =
	
	PlatformUI
		.getWorkbench()
		.getPerspectiveRegistry()
		.findPerspectiveWithId(
			"org.reliablesource.client.ui.symbiosys.platform.perspective");
	
	PlatformUI
		.getWorkbench()
		.getActiveWorkbenchWindow()
		.getActivePage()
		.setPerspective(_pd);
    }

    @Override
    public void postWindowClose() {
	super.postWindowClose();

	if (services == null)
	    services = PlatformUI.getWorkbench().getActiveWorkbenchWindow();

	if (contextService == null)
	    contextService = (IContextService) services
		    .getService(IContextService.class);

	if (contextToken != null)
	    contextService.deactivateContext(contextToken);
    }

    private void setContext() {
	if (services == null)
	    services = PlatformUI.getWorkbench().getActiveWorkbenchWindow();

	if (contextService == null)
	    contextService = (IContextService) services
		    .getService(IContextService.class);

	if (contextService != null)
	    contextToken = contextService.activateContext(WINDOW_CONTEXT_ID);
    }
}
